<template>
  <div class="login-container">
    <!-- <div class="imgBack"> -->
    <!-- <img src="@/assets/img/radiation-11.png"> -->
    <!-- </div> -->
    <div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <h3 class="title">
          后台管理系统
        <!-- <img src="@/assets/img/radiation-13.png"> -->
        </h3>
        <div v-if="isAccount">
          <el-form-item prop="username">
            <img src="@/assets/img/radiation-14.png" alt class="imgInput">
            <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <img src="@/assets/img/radiation-15.png" alt class="imgInput">
            <el-input
              :type="pwdType"
              v-model="loginForm.password"
              name="password"
              auto-complete="on"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </el-form-item>
        </div>
        <div v-if="!isAccount" style="text-align: center;">
          <img :src="imgUrl" style="width: 200px;">
        </div>
        <!-- <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click="handleLogin">
          登录
        </el-button>
      </el-form-item> -->
        <div v-if="isAccount">
          <el-button
            :loading="loading"
            type="primary"
            class="buttonLogin"
            @click="handleLogin"
          >登 录</el-button>
          <el-button
            style="margin-top: 20px;"
            type="primary"
            class="buttonLogin"
            @click="codeLogin"
          >二维码登录</el-button>
        </div>
        <div v-if="!isAccount">
          <el-button
            style="margin-top: 20px;"
            type="primary"
            class="buttonLogin"
            @click="goBack"
          >返回</el-button>
        </div>
        <!--      <div class="tips">-->
        <!--        <span style="margin-right:20px;">username: admin</span>-->
        <!--        <span> password: 111111</span>-->
        <!--      </div>-->
      </el-form>
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import user from '../../api/acl/user.js'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      timer: '',
      key: '',
      isAccount: true,
      imgUrl: '',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    codeLogin() {
      const that = this
      user.getWxQrCode({
      })
        .then((response) => {
          console.log(response)
          this.key = response.key
          this.imgUrl = response.url
          this.isAccount = false
          this.timer = setInterval(() => {
            // user.qrCodePolling({
            //   key: response.key
            // })
            this.$store.dispatch('qrCodePolling', {
              key: response.key
            })
              .then((responseOne) => {
                console.log(responseOne)
                if (window.sessionStorage.getItem('roleName').split(',').length == 0) {
                  that.$message({
                    message: '无登录权限',
                    duration: 1000
                  })
                } else {
                  if (responseOne) {
                    clearInterval(that.timer)
                    that.$message({
                      message: '登录成功',
                      duration: 1000
                    })
                    // this.$router.push({ path: this.redirect || '/' })
                    that.$router.push({ path: '/' })
                  }
                }
              })
          }, 1000)
        })
    },
    goBack() {
      this.isAccount = true
      clearInterval(this.timer)
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // debugger
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            if (window.sessionStorage.getItem('roleName').split(',').length == 0) {
              this.$message({
                message: '无登录权限',
                duration: 1000
              })
            } else {
              this.$message({
                message: '登录成功',
                duration: 1000
              })
              clearInterval(this.timer)
              // this.$router.push({ path: this.redirect || '/' })
              this.$router.push({ path: '/' })
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 45px;
    width: 75%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 5px;
      // color: $light_gray;
      color:#47BDD2;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #f1f3f4 inset !important;
        -webkit-text-fill-color:#47BDD2 !important;
      }
    }
  }
  .el-form-item {
    width: 336px;
    height: 50px;
    border-radius: 5px;
    color: #454545;
    margin: 0 auto 22px;
    border: 1px solid #d9d9d9;
        background: #fff;
    img {
      margin: 0 10px;
    }
    // border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    // color: #454545;
  }
    .el-form-item__label {
    color: #fff;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
   min-height: 100%;
  width: 100%;
  // background-image: url("../../assets/img/radiation.png");
  // background-repeat: no-repeat;
  // background-size: 100% 100%;
  // -moz-background-size: 100% 100%;
  overflow: hidden;
  // position: fixed;
  // height: 100%;
  // width: 100%;
  background-color: $bg;
  .imgBack {
    width: 650px;
    height: 550px;
    position: absolute;
    top: 50px;
    left: 100px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .login-form {
    position: relative;
    width: 420px;
    height: 420px;
    margin: 13% 0 0 60%;
    overflow: hidden;
    border-radius: 6px;
    // position: absolute;
    // left: 0;
    // right: 0;
    // width: 520px;
    // max-width: 100%;
    // padding: 35px 35px 15px 35px;
    // margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #ccc;
    margin: 15px 0;
    margin-bottom: 10px;
    text-align: right;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
    img{
      width:90%;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .buttonLogin{
    width:80%;
    margin: 0px 0 0 10%;
    background:linear-gradient(to right,blue,#47BDD2);
    border: none;
    font-size: 16px;
  }
  .imgInput{
        float: left;
    margin: 12px 0px 12px 5px;
    width: 25px;
  }
}
</style>
